package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.domain.AddCartGoodsReq;
import com.interview.one.domain.DeleteCartGoodsReq;
import com.interview.one.domain.InitAction;
import com.interview.one.domain.SubmitCartReq;
import com.interview.one.model.CartGoods;
import com.interview.one.model.OrderGoods;
import com.interview.one.model.OrderInfo;
import com.interview.one.model.ShipAddress;
import com.interview.one.service.CartGoodsService;
import com.interview.one.service.OrderService;
import com.interview.one.service.ShipAddressService;
import com.interview.one.utils.YuanFenConverter;
import org.aspectj.weaver.patterns.IToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartGoodsController {
    @Autowired
    private CartGoodsService cartGoodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShipAddressService shipAddressService;

    @PostMapping("/getList")
    public CommonRes getList( @RequestHeader String token) throws BusinessException {
        if ((token == null) || (token.equals(""))) {
            throw new BusinessException(EmBusinessError.NOT_LOGIN_FAIL);
        }

        List list = this.cartGoodsService.getCartGoodsList(Integer.valueOf(token));
        if ((list == null) || (list.size() == 0)) {
            throw new BusinessException(EmBusinessError.NO_DATA_ERROR);
        }
        return CommonRes.create(list);
    }

    @PostMapping("/add")
    public CommonRes  addCartGoods(@RequestBody AddCartGoodsReq req, @RequestHeader String token){
        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodsId(req.getGoodsId());
        cartGoods.setUserId(Integer.valueOf(token));
        cartGoods.setGoodsIcon(req.getGoodsIcon());
        cartGoods.setGoodsDesc(req.getGoodsDesc());
        cartGoods.setGoodsPrice(req.getGoodsPrice());
        cartGoods.setGoodsCount(req.getGoodsCount());
        cartGoods.setGoodsSku(req.getGoodsSku());

        this.cartGoodsService.addCartGoods(cartGoods);
        return CommonRes.create(cartGoods);
    }

    @PostMapping("/delete")
    public CommonRes deleteCartGoods(@RequestBody DeleteCartGoodsReq req) {
        this.cartGoodsService.deleteCartGoods(req.getCartIdList());
        return CommonRes.create("删除购物车成功");
    }

    @PostMapping("/submit")
    public CommonRes submitCart(@RequestBody SubmitCartReq req, @RequestHeader String token){
        int userId = Integer.valueOf(token);

        OrderInfo orderInfo = new OrderInfo();

        orderInfo.setUserId(userId);
        try {

            orderInfo.setTotalPrice(Integer.parseInt(YuanFenConverter.changeF2Y(req.getTotalPrice())));
        } catch (Exception e) {

            e.printStackTrace();
        }


        List<ShipAddress> shipList = this.shipAddressService.getShipAddress(Integer.valueOf(userId));

        if ((shipList == null) || (shipList.size() == 0))
            orderInfo.setShipId(Integer.valueOf(0));
        else {

            for (ShipAddress shipAddress : shipList) {

                if (shipAddress.getShipIsDefault().intValue() == 0) {

                    orderInfo.setShipId(shipAddress.getId());
                }
            }
        }

        orderInfo.setOrderStatus(Integer.valueOf(-1));

        orderInfo.setPayType(Integer.valueOf(0));

        int orderId = this.orderService.addOrder(orderInfo);

        List cartIdList = new ArrayList();

        for (OrderGoods orderGoods : req.getGoodsList()) {

            cartIdList.add(orderGoods.getId());

            orderGoods.setOrderId(Integer.valueOf(orderId));
            try {

                orderGoods.setGoodsPrice(YuanFenConverter.changeF2Y(orderGoods.getGoodsPrice()));
            } catch (Exception e) {

                e.printStackTrace();
            }

            this.orderService.addOrderGoods(orderGoods);
        }


        InitAction.cartIdMap.put(Integer.valueOf(orderId), cartIdList);
        return CommonRes.create(orderId);
    }


}
